package com.huluobo.kotlinim1907a.ui.activity

import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import com.huluobo.kotlinim1907a.R
import com.huluobo.kotlinim1907a.base.BaseActivity
import com.huluobo.kotlinim1907a.contract.login.LoginContract
import com.huluobo.kotlinim1907a.contract.login.LoginPresenter
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * Created by lc on 2022/1/11.
 */
class LoginActivity : BaseActivity(), LoginContract.View {

    private val presenter = LoginPresenter(this)

    override fun init() {
        super.init()
        login.setOnClickListener {
            login()
        }

        password.setOnEditorActionListener { _, _, _ ->
            login()
            true
        }

//        //上下两个方法等价
//        password.setOnEditorActionListener(object : TextView.OnEditorActionListener {
//            override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
//                login()
//                return true
//            }
//        })
        newUser.setOnClickListener {
            startActivity<RegisterActivity>()
        }
    }

    private fun login() {
        if (hasWriteExternalStoragePermission()) {
            hideSoftKeyboard()
            val userNameString = userName.text.trim().toString()
            val passwordString = password.text.trim().toString()
            presenter.login(userNameString, passwordString)
        } else applyWriteExternalStoragePermission()
    }

    //让用户同意给出的权限
    private fun applyWriteExternalStoragePermission() {
        val permission = arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
        ActivityCompat.requestPermissions(this, permission, 0)
    }

    //给出权限之后的回调
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //如果用户同意授权,开始登录
            login()
        } else toast(R.string.permission_denied)
    }

    //检查是否有写入的权限
    private fun hasWriteExternalStoragePermission(): Boolean {
        //检查自身权限
        val checkSelfPermission = ActivityCompat.checkSelfPermission(
            this,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        //返回是否在当前权限组中有写入的权限
        return checkSelfPermission == PackageManager.PERMISSION_GRANTED
    }

    override fun getLayoutResId(): Int = R.layout.activity_login
    override fun onUserNameError() {
        //显示出现错误的文字
        userName.error = getString(R.string.user_name_error)
    }

    override fun onPasswordError() {
        //显示出现密码错误的文字
        password.error = getString(R.string.password_error)
    }

    override fun onStartLogin() {
        //弹出进度条
        showProgress(getString(R.string.logging))
    }

    override fun onLoggedInSuccess() {
        //隐藏进度条
        dismissProgress()
        //进入主页面
        startActivity<MainActivity>()
        //退出当前activity
        finish()
    }

    override fun onLoggedInFailed() {
        //隐藏进度条
        dismissProgress()
        //弹出toast提示(1,判断为什么会失败,2,给出重新登录的建议)
        toast(getString(R.string.login_failed))
    }
}